package cn.tl.org.service.impl;

import cn.tl.org.domain.Dept;
import cn.tl.org.mapper.DeptMapper;
import cn.tl.org.query.DeptQuery;
import cn.tl.org.service.IDeptService;
import cn.tl.org.utils.QueryResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DeptServiceImpl implements IDeptService {
    @Autowired
    DeptMapper deptMapper;
    @Override
    public List<Dept> findAll() {
        return deptMapper.findAll();
    }

    @Override
    public Dept findById(Long id) {
        return deptMapper.findById(id);
    }

    @Override
    @Transactional
    public void save(Dept dept) {
        deptMapper.save(dept);
        //判断上级部门是否为空
        if(dept.getParent()==null){
            dept.setDirPath("/" + dept.getId());
        }else {
            Dept parent = deptMapper.findById(dept.getParent().getId());
            dept.setDirPath(parent.getDirPath() + "/" + dept.getId());
        }

        deptMapper.update(dept);
    }

    @Override
    @Transactional
    public void update(Dept dept) {
        deptMapper.update(dept);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        deptMapper.delete(id);
    }

    @Override
    public QueryResult<Dept> queryPage(DeptQuery query) {
        Long count = deptMapper.count(query);
        List<Dept> data = deptMapper.data(query);

        return new QueryResult<>(count,data);
    }

    @Override
    public void patchDel(Long[] ids) {
        deptMapper.patchDel(ids);
    }

    @Override
    public List<Dept> loadDeptTree() {
        return deptMapper.loadDeptTree();
    }
}
